package com.amumu.netctoss.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.amumu.netctoss.dao.AccountDao;
import com.amumu.netctoss.entity.Account;
@Repository
@Scope("prototype")
public class HibernateAccountDao extends HibernateBaseDao implements AccountDao {

	public void delete(int id) throws Exception {
		Account a = findById(id);
		a.setStatus("2");
		a.setCloseDate(new Date(System.currentTimeMillis()));
		executeUpdate("UPDATE Service SET status=2,closeDate=current_date() WHERE account.id=?", id);

	}

	public List<Account> findAll(int page, int size) throws Exception {
		return findByPage(page, size, "from Account", null);
	}

	public Account findById(int id) throws Exception {
		return (Account)getHibernateTemplate().load(Account.class, id);
	}

	public Account findByIdcard(String idcardNo) throws Exception {
		return (Account) uniqueResult("from Account where idcardNo=?", idcardNo);
	}

	public String findIdcardNo(Integer id) throws Exception {
		return (String) uniqueResult("select idcardNo from Account where id=?", id);
	}

	public String findPassword(int id) throws Exception {
		return (String) uniqueResult("select loginPassword from Account where id=?", id);
	}

	public Map<String, Object> getOccupationOptions() throws Exception {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put(null, "");
		map.put("干部", "干部");
		map.put("学生", "学生");
		map.put("技术人员", "技术人员");
		map.put("其它", "其它");
		return map;
	}

	public int getPageCount(int size) throws Exception {
		return getTotalPage(size, "select count(*) from Account", null); 
	}

	public int getPageCount(int size, String idcardNo, String realName,
			String loginName, String status) throws Exception {
		List params = new ArrayList();
		StringBuilder hql = new StringBuilder("select count(*) from Account where 1=1");
		if(!"".equals(idcardNo)&&idcardNo!=null){
			hql.append(" and idcardNo=? ");
			params.add(idcardNo);
		}
		if(!"".equals(realName)&&realName!=null){
			hql.append(" and realName like ? ");
			params.add("%"+realName+"%");
		}
		if(!"".equals(loginName)&&loginName!=null){
			hql.append(" and loginName=? ");
			params.add(loginName);
		}
		if(!"".equals(status)&&status!=null){
			hql.append(" and status=? ");
			params.add(status);
		}
		return getTotalPage(size, hql.toString(), params.toArray());
	}

	public Map<String, Object> getStatusOptions() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("0", "开通");
		map.put("1", "暂停");
		map.put("2", "删除");
		return map;
	}

	public boolean hasLoginName(String name) throws Exception {
		Object obj = uniqueResult("from Account where loginName=?", name);
		return obj!=null;
	}

	public void save(Account act) throws Exception {
		act.setStatus("1");
		act.setCreateDate(new Date(System.currentTimeMillis()));
		getHibernateTemplate().save(act);

	}

	public List<Account> search(int page, int size, String idcardNo,
			String realName, String loginName, String status) throws Exception {
		List params = new ArrayList();
		StringBuilder hql = new StringBuilder("from Account where 1=1");
		if(!"".equals(idcardNo)&&idcardNo!=null){
			hql.append(" and idcardNo=? ");
			params.add(idcardNo);
		}
		if(!"".equals(realName)&&realName!=null){
			hql.append(" and realName like ? ");
			params.add("%"+realName+"%");
		}
		if(!"".equals(loginName)&&loginName!=null){
			hql.append(" and loginName=? ");
			params.add(loginName);
		}
		if(!"".equals(status)&&status!=null){
			hql.append(" and status=? ");
			params.add(status);
		}
		return findByPage(page, size, hql.toString(), params.toArray());
	}

	@Transactional
	public void setStatus(int id, String status) throws Exception {
		Account a = findById(id);
		a.setStatus(status);
		if(status.equals("0")){
			a.setPauseDate(null);
			//hql = "UPDATE Account SET status=0,pauseDate=null WHERE id=?";
		}else{
			a.setPauseDate(new Date(System.currentTimeMillis()));
			//hql = "UPDATE Account SET status=1,pauseDate=current_date() WHERE id=?";
		}
		if(status.equals("1")){
			executeUpdate("UPDATE Service SET status=1,pauseDate=current_date() WHERE account.id=?", id);
		}

	}

	public void update(Account act) throws Exception {
		Account temp = findById(act.getId());
		act.setStatus(temp.getStatus());
		act.setPauseDate(temp.getPauseDate());
		getHibernateTemplate().evict(temp);
		getHibernateTemplate().update(act);

	}

}
